package cn.dnui_lf_705.dnui_16110100705.controller;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.List;

import cn.dnui_lf_705.dnui_16110100705.Adapter.FoodListAdapter;
import cn.dnui_lf_705.dnui_16110100705.R;
import cn.dnui_lf_705.dnui_16110100705.bean.FoodInfo;
import cn.dnui_lf_705.dnui_16110100705.listener.OnRetrofitListener;
import cn.dnui_lf_705.dnui_16110100705.model.FoodModel;


public class SearchResultActivity extends BaseActivity implements OnRetrofitListener<List<FoodInfo>> {
    private RecyclerView recyclerView;
    private String search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        initViews();
        initData();
        if (setEnableSliding()) {
            ArActSlidLayout rootView = new ArActSlidLayout(this);
            rootView.bindActivity(this);//绑定需要窗口的布局：
        }
    }
    protected boolean setEnableSliding() {//默认返回是需要的，只需继承的时候重写次代码 true : 需要；false :不需要；
        return true;
    }






    @Override
    void initViews() {
        recyclerView = findViewById(R.id.result_rv);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    void initEvents() {
            //处理返回按钮
    }

    @Override
    void initData() {
        Intent intent = getIntent();
        search = intent.getStringExtra("search");
        FoodModel foodModel = new FoodModel();
        foodModel.getFoodBySearch(search,this);
    }


    @Override
    public void onsuccess(List<FoodInfo> list, int flag) {
            if(list != null &&list.size()>0){
                FoodListAdapter adapter = new FoodListAdapter(SearchResultActivity.this,list);
                recyclerView.setAdapter(adapter);
            }
            else
            {
                showToast("没有搜索结果");
                finish();
            }
    }

    @Override
    public void onFailure(String string) {
            showToast("网络错误搜索失败");
    }
}
